<template>
  <div class="app-container">
    <el-form :inline="true">
      <el-form-item label="角色名称">
        <el-input v-model="pageInfo.roleName" placeholder="请输入" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="delbath">删除</el-button>
    <el-button type="primary" @click="toadd">添加</el-button>
    <el-button type="primary" @click="toupdate">修改</el-button>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        prop="roleId"
        label="角色ID"
        width="180"
      />
      <el-table-column
        prop="roleName"
        label="角色名称"
        width="180"
      />
      <el-table-column
        prop="dept.name"
        label="所属部门"
      />
      <el-table-column
        prop="remark"
        label="备注"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
      />
    </el-table>

    <el-pagination
      :current-page="pageInfo.pageNum"
      :page-sizes="[10,15,20,25]"
      :page-size="pageInfo.pagerSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <el-dialog title="编辑角色" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.roleName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input v-model="form.remark" autocomplete="off" />
        </el-form-item>
        <el-form-item label="所属部门" :label-width="formLabelWidth">
          <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick" />
        </el-form-item>

        <el-form-item label="功能权限" :label-width="formLabelWidth">
          <el-tree
            ref="tree"
            :data="datas"
            show-checkbox
            node-key="menuId"
            highlight-current
            :props="defaultPropss"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      selected: [],
      tableData: [],
      total: null,
      dialogFormVisible: false,
      formLabelWidth: '200px',
      flag: '',
      data: [],
      datas: [],
      pageInfo: {
        pageNum: null,
        pagerSize: null,
        roleName: ''
      },
      // 部门tree加载
      defaultProps: {
        children: 'depts',
        label: 'name'
      },
      // 菜单权限加载
      defaultPropss: {
        children: 'menuList',
        label: 'name'
      },
      form: {
        roleId: null,
        roleName: '',
        remark: '',
        deptId: null,
        createTime: '2001-08-09',
        parentId: null,
        mids: []
      }
    }
  },
  created() {
    this.getlist()
  },
  methods: {
    getlist() {
      this.axios.get('http://localhost:8091/role/list', { params: this.pageInfo }).then(res => {
        this.tableData = res.data.list
        this.pageInfo.pagerSize = res.data.pageSize
        this.pageInfo.pageNum = res.data.pageNum
        this.total = res.data.total
      })
      // 部门tree
      this.axios.get('http://localhost:8091/role/getDeptss').then((res) => {
        this.data = res.data.obj
      })
      // 菜单tree
      this.axios.get('http://localhost:8091/menu/list').then((res) => {
        this.datas = res.data
      })
    },
    toadd() {
      this.dialogFormVisible = true
      this.flag = 'add'
    },
    toupdate() {
      if (this.selected.length > 1 || this.selected.length < 1) {
        alert('请选择一条数据！！！')
      } else {
        this.flag = 'update'
        this.dialogFormVisible = true
        this.axios.get('http://localhost:8091/role/getOne?id=' + this.selected[0]).then(res => {
          alert(res)
          this.form.roleId = res.data.roleId
          this.form.roleName = res.data.roleName
          this.form.remark = res.data.remark
          this.form.deptId = res.data.deptId
          this.form.parentId = res.data.parentId
          const mIds = []
          for (const m of res.data.mlists) {
            mIds.push(m.menuId)
          }
          this.form.mids = mIds
          this.$refs.tree.setCheckedKeys(mIds)
        })
      }
    },
    save() {
      var url = ''
      if (this.flag === 'add') {
        url = 'http://localhost:8091/role/roleadd'
      } if (this.flag === 'update') {
        url = 'http://localhost:8091/role/roleupdate'
      }
      this.form.mids = this.$refs.tree.getCheckedKeys()
      alert(this.$refs.tree.getCheckedKeys())
      this.axios.post(url, this.form).then(res => {
        if (res.data) {
          alert('保存成功')
          this.form = []
          this.dialogFormVisible = false
          this.$router.go(0)
        } else {
          alert('保存失败')
        }
      })
    },
    handleSelectionChange(rows) {
      this.selected = []
      for (const row of rows) {
        this.selected.push(row.roleId)
      }
    },
    delbath() {
      var ids = this.selected.join(',')
      alert(ids)
      this.$confirm('确认要删除吗').then(res => {
        this.axios.get('http://localhost:8091/role/delBath?ids=' + ids).then(res => {
          if (res) {
            alert('删除成功')
            this.getlist()
          } else {
            alert('删除失败')
          }
        })
      })
    },
    onSubmit() {
      this.pageInfo.pageNum = 1
      this.getlist()
    },
    handleSizeChange(val) {
      this.pageInfo.pagerSize = val
      this.getlist()
    },
    handleCurrentChange(val) {
      this.pageInfo.pageNum = val
      this.getlist()
    },
    handleNodeClick(data) {
      console.log(data)
      this.form.deptId = data.deptId
      this.form.parentId = data.parentId
    }
  }
}
</script>

